package com.uenpay.im.ui.old.activity.company;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.qyx.android.entity.FriendEntity;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.ui.old.activity.contacts.ChooseContactsActivity;

/**
 * @author SL
 * @created 2016-2-23 下午2:28:41
 * @copyRight http://www.qiyunxin.com
 * @function 添加员工
 */
public class AddEmployeeActivity extends Activity {

	private EditText position_et, phone_number_et, name_et;
	private TextView department_tv;
	private int entid, departid;
	private View loading;
	private CompanyHandle mCompanyHandle = new CompanyHandle();
	private Button delete_employee_btn;
	private String custid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IMApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_add_employee_layout);

		initView();
		initListener();
		if (getIntent() != null && getIntent().hasExtra("entid")) {
			entid = getIntent().getIntExtra("entid", 0);
			departid = getIntent().getIntExtra("departid", 0);
			String superior_department = getIntent().getStringExtra(
					"superior_department");
			if (!TextUtils.isEmpty(superior_department)) {
				department_tv.setText(superior_department);
			}
		}

		if (getIntent() != null && getIntent().hasExtra("employeeEntity")) {
			EmployeeEntity employeeEntity = (EmployeeEntity) getIntent()
					.getSerializableExtra("employeeEntity");
			if (employeeEntity != null) {
				custid = employeeEntity.custid;
				((TextView) findViewById(R.id.title_textview))
						.setText(R.string.edit_employee);
				position_et.setText(employeeEntity.position);
				phone_number_et.setText(employeeEntity.mobile);
				name_et.setText(employeeEntity.nickname);
				delete_employee_btn.setVisibility(View.VISIBLE);
				phone_number_et.setEnabled(false);
			}
		}
	}

	private void initView() {
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.add_employee);
		department_tv = (TextView) findViewById(R.id.department_tv);
		position_et = (EditText) findViewById(R.id.position_et);
		phone_number_et = (EditText) findViewById(R.id.phone_number_et);
		name_et = (EditText) findViewById(R.id.name_et);
		findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
		((TextView) findViewById(R.id.title_right_tv)).setText(R.string.save);
		loading = findViewById(R.id.loading);
		delete_employee_btn = (Button) findViewById(R.id.delete_employee_btn);
	}

	private void initListener() {
		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});
		findViewById(R.id.title_right_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO
						String name = name_et.getText().toString();
						String phone = phone_number_et.getText().toString();
						String position = position_et.getText().toString();
						if (!TextUtils.isEmpty(name)
								&& !TextUtils.isEmpty(phone)) {
							if (!TextUtils.isEmpty(custid)) {
								updateEmployee(name, position);
							} else {
								addEmployee(phone, name, position);
							}
						}
					}
				});
		findViewById(R.id.choose_contacts_iv).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Bundle bundle = new Bundle();
						bundle.putBoolean("isChooseCrad", true);
						ActivityHandle.startActivity(AddEmployeeActivity.this,
								ChooseContactsActivity.class, bundle, 100);
					}
				});
		delete_employee_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (!TextUtils.isEmpty(custid)) {
					deleteEmployee(custid);
				}
			}
		});
	}

	private void addEmployee(String mobile, String nickname, String position) {
		loading.setVisibility(View.VISIBLE);
		mCompanyHandle.addEmployee(departid, entid, mobile, nickname, position,
				new CompanyHandle.ICompanyResultListener() {

					@Override
					public void onResult(int status, String msg) {
						loading.setVisibility(View.GONE);
						if (status == HttpInvokeResultCode.RESULT_OK) {
							setResult(RESULT_OK);
							Utils.hideSoftKeyboard(name_et);
							finish();
						} else if (!TextUtils.isEmpty(msg)) {
							IMApplication.showToast(msg);
						}
					}
				});
	}

	private void updateEmployee(String nickname, String position) {
		loading.setVisibility(View.VISIBLE);
		mCompanyHandle.updateEmployee(custid, departid, entid, position,
				nickname, new CompanyHandle.ICompanyResultListener() {

					@Override
					public void onResult(int status, String msg) {
						loading.setVisibility(View.GONE);
						if (status == HttpInvokeResultCode.RESULT_OK) {
							setResult(RESULT_OK);
							Utils.hideSoftKeyboard(name_et);
							finish();
						} else if (!TextUtils.isEmpty(msg)) {
							IMApplication.showToast(msg);
						}
					}
				});
	}

	private void deleteEmployee(String custids) {
		loading.setVisibility(View.VISIBLE);
		mCompanyHandle.deleteEmployee(entid, departid, custids,
				new CompanyHandle.ICompanyResultListener() {

					@Override
					public void onResult(int status, String msg) {

						loading.setVisibility(View.GONE);
						if (status == HttpInvokeResultCode.RESULT_OK) {
							setResult(RESULT_OK);
							finish();
						} else if (!TextUtils.isEmpty(msg)) {
							IMApplication.showToast(msg);
						}

					}
				});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 100 && resultCode == RESULT_OK) {
			FriendEntity friendEntity = (FriendEntity) data
					.getSerializableExtra("selectedUser");
			if (friendEntity != null
					&& !TextUtils.isEmpty(friendEntity.nick_name))
				name_et.setText(friendEntity.nick_name);
			if (friendEntity.cust_id.equals(IMApplication.getCustId())) {
				phone_number_et.setText(IMApplication.getUserMobilePhone());
			} else {
				phone_number_et.setText(friendEntity.mobile);
			}
			Utils.showSoftKeyboard(phone_number_et);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}
}
